﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

using Game.Command;
using Game.Controller;

namespace SL
{
    public class ButtonEnlarger : DependencyObject
    {
        public static readonly DependencyProperty ScaleProperty = DependencyProperty.Register(
            "Scale", typeof(Double), typeof(ButtonEnlarger), new PropertyMetadata(0d, OnScaleChanged));

        static void OnScaleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var p = d as ButtonEnlarger;
            var g = new ScaleTransform();
            g.ScaleX = g.ScaleY = GetScale(p);
            g.CenterX = p.Button.ActualHeight / 2;
            g.CenterY = p.Button.ActualHeight / 2;
            p.Button.RenderTransform = g;
        }

        public static double GetScale(ButtonEnlarger element)
        {
            return (double)element.GetValue(ScaleProperty);
        }

        public static void SetScale(ButtonEnlarger element, double value)
        {
            element.SetValue(ScaleProperty, value);
        }

        public readonly static double Size = 100;

        readonly Button _Button;
        readonly GameCommand _GameCommand;
        readonly GameController _GameController;

        public ButtonEnlarger(
            Button button, 
            GameCommand command,
            GameController controller)
        {
            _Button = button;
            _GameCommand = command;
            _GameController = controller;

            _GameController.CommandExecutor.After += (source, args) => EnableButtons();
            _Button.MouseLeave += (source, args) => MakeSmall();
            _Button.MouseEnter += (source, args) => MakeLarge();

            EnableButtons();
        }

        void EnableButtons()
        {
            if (_GameCommand.CanExecute())
            {
                _Button.IsEnabled = true;
            }
            else
            {
                _Button.IsEnabled = false;
            }
        }

        public Button Button
        {
            get { return _Button; }
        }

        public GameCommand GameCommand
        {
            get { return _GameCommand; }
        }

        void MakeLarge()
        {
            if (!CanZoom())
            {
                SetScale(this, 1);
                return;
            }

            StartAutomation(1.0, 1.3, 0.2);
        }

        void MakeSmall()
        {
            if (!CanZoom())
            {
                if (GetScale(this) > 1)
                {
                    StartAutomation(1.3, 1.0, 1);
                }

                return;
            }

            StartAutomation(1.3, 1.0, 1);
        }

        bool CanZoom()
        {
            return true;
        }

        void StartAutomation(double start, double end, double time)
        {
            var a = new DoubleAnimation();
            a.From = start;
            a.To = end;

            var duration = new Duration(TimeSpan.FromSeconds(time));
            a.EasingFunction = new ExponentialEase();
            a.Duration = duration;

            var sb = new Storyboard();
            sb.Duration = duration;
            sb.Children.Add(a);

            Storyboard.SetTarget(a, this);
            Storyboard.SetTargetProperty(a, new PropertyPath("Scale"));

            sb.Begin();
        }
    }
}
